package com.cortex.pineapple.view;

import java.text.DecimalFormat;
import java.util.List;

import com.cortex.pineapple.R;
import com.cortext.pineapple.link.ImageLink;
import com.squareup.picasso.Picasso;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class GalleryImagesView extends FrameLayout {

	private ViewFlipper flipperImage;
	private ImageView nextImage,previousImage;
	private ImageView close;
	private TextView title,dimensions,sizeFile,type,url;
	private List<ImageLink> linksImage;
	private ImageLink linkSelected;
	private Animation rightIn,rightOut;
	private Animation leftIn,leftOut;
	private OnClickUrlListener onClickUrlListener;
	private OnCloseListener onCloseListener;

	public GalleryImagesView(Context context) {
		super(context);
		init();
	}
	public GalleryImagesView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}
	public GalleryImagesView(Context context, AttributeSet attrs,int style) {
		super(context, attrs,style);
		init();
	}
	private void init(){
		LayoutInflater.from(getContext()).inflate(R.layout.view_gallery_images, this);
		flipperImage = (ViewFlipper)findViewById(R.id.flipperImage);
		nextImage = (ImageView)findViewById(R.id.nextImage);
		previousImage = (ImageView)findViewById(R.id.previousImage);
		title = (TextView)findViewById(R.id.title);
		dimensions = (TextView)findViewById(R.id.dimensions);
		sizeFile = (TextView)findViewById(R.id.sizeFile);
		type = (TextView)findViewById(R.id.type);
		url = (TextView)findViewById(R.id.url);
		close = (ImageView)findViewById(R.id.close);
		rightIn = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_and_fade_in);
		rightOut = AnimationUtils.loadAnimation(getContext(), R.anim.push_right_and_fade_out);
		leftIn = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_and_fade_in);
		leftOut = AnimationUtils.loadAnimation(getContext(), R.anim.push_left_and_fade_out);
		initListeners();
	}
	private void initListeners(){
		nextImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				flipperImage.setInAnimation(leftIn);
				flipperImage.setOutAnimation(rightOut);
				flipperImage.showNext();
				int newIndexSelected = linksImage.indexOf(linkSelected) + 1;
				setSelectedImage(newIndexSelected);
			}
		});
		previousImage.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				flipperImage.setInAnimation(rightIn);
				flipperImage.setOutAnimation(leftOut);
				flipperImage.showPrevious();
				int newIndexSelected = linksImage.indexOf(linkSelected) - 1;
				setSelectedImage(newIndexSelected);
			}
		});
		url.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if(onClickUrlListener != null){
					onClickUrlListener.clickUrl(linkSelected);
				}
			}
		});
		close.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				setVisibility(GONE);
				if(onCloseListener != null){
					onCloseListener.closedView();
				}
			}
		});
	}
	public void setOnClickUrlListener(OnClickUrlListener onClickUrlListener) {
		this.onClickUrlListener = onClickUrlListener;
	}
	public void setOnCloseListener(OnCloseListener onCloseListener) {
		this.onCloseListener = onCloseListener;
	}
	public void show(List<ImageLink> linksImage,ImageLink selectedLink){
		this.linksImage = linksImage;
		this.linkSelected = selectedLink;
		buildGallery();
		setVisibility(VISIBLE);
	}
	private void buildGallery(){
		flipperImage.removeAllViews();
		for (ImageLink imageLink : linksImage) {
			ImageView image = new ImageView(getContext());

			image.setLayoutParams(new FrameLayout.LayoutParams
					(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT));
			Picasso.with(getContext())
			.load(imageLink.getImageUrl())
			.noFade()
			.into(image);
			flipperImage.addView(image);
		}
		int indexSelected = linksImage.indexOf(linkSelected);
		flipperImage.setDisplayedChild(indexSelected);
		setSelectedImage(indexSelected);
	}
	private void setSelectedImage(int index){
		if(index == linksImage.size()){
			index = 0;
		}
		if(index == -1){
			index = linksImage.size() - 1;
		}
		linkSelected = linksImage.get(index);
		setInfoImage(linkSelected);
		//		for (int i = 0; i < flipperImage.getChildCount(); i++) {
		//			((ImageView)flipperImage.getChildAt(i)).setImageBitmap(null);
		//		}
		//		Picasso.with(getContext())
		//		.load(linkSelected.getImageUrl())
		//		.noFade()
		//		.into((ImageView)flipperImage.getChildAt(index));
	}
	private void setInfoImage(ImageLink imageLink){
		title.setText(imageLink.getTitle());
		dimensions.setText(imageLink.getWidth() + " X " + imageLink.getHeight());
		sizeFile.setText(new DecimalFormat("##.##").format(imageLink.getFileSize()) + "m");
		type.setText(imageLink.getType());
		url.setText(imageLink.getDisplayUrl());
	}
	public interface OnClickUrlListener{
		void clickUrl(ImageLink link);
	}
	public interface OnCloseListener{
		void closedView();
	}
	public ImageLink getImageShow() {
		return linkSelected;
	}
}
